package info.androidhive.tabsswipe;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


import com.example.newscrolling.enity.Movie;
import com.example.newscrolling.service.UserDAO;

import info.androidhive.tabsswipe.R;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class NowFragment extends Fragment {
	
	private ListView listView3;
	private List<Movie> movieList;
	private UserDAO user3Dao;
	private static final String TAG = "NowFragment";
	private int selectedIndex;
	private int selectedMode;
	private String[] modeList = {"影片介紹","加入我的最愛"};
	private Movie movie;
	private ArrayList<HashMap> arrayList;
	private int selected;
	
	public NowFragment(int selected) {
		this.selected = selected;
	}
	
	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View rootView = inflater.inflate(R.layout.fragment_now, container, false);
		
		return rootView;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) 
	{
		super.onActivityCreated(savedInstanceState);
		initView();
		initHandler();
	}
	private void initHandler() {
		// TODO Auto-generated method stub
		listView3.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				// TODO Auto-generated method stub
				selectedIndex = position;
				AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());
				dialog.setTitle("請選擇");
				dialog.setSingleChoiceItems(modeList,-1,new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog,int which) {
						// TODO Auto-generated method stub
						selectedMode = which;
					}
				});
						dialog.setPositiveButton("ok", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						String selectedText = modeList[selectedMode];
						Intent it = new Intent();
						Bundle bundle = new Bundle();
						bundle.putInt("M_POSITION",selectedIndex);
						it.putExtras(bundle);
						it.setClass(getActivity(), DetailActivity.class);NowFragment.this.startActivity(it);
						
					}
				});
					dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						
					}
				});
				dialog.show();
				
			}
		});
	}

	private void initView() {
		// TODO Auto-generated method stub
		UserDAO user3Dao = new UserDAO(getActivity()); 
		listView3 = (ListView)getActivity().findViewById(R.id.listView3);
		//movieList = user3Dao.getAll(selected);
		arrayList = user3Dao.getAllm(selected);
		listView3.setAdapter(new MovieBaseAdapter(getActivity(),arrayList));
		
	}
	
	
	
}
